package com.visionblue.business.ship.page.collection.panel.flow

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.visionblue.business.ship.R
import com.visionblue.business.ship.model.bean.local.OilFlowInfo
import com.visionblue.business.ship.model.bean.local.UltraSonicFlowInfo
import com.visionblue.business.ship.model.bean.local.WaterFlowInfo
import com.visionblue.business.ship.model.enums.WasteTypeEnum
import com.visionblue.business.ship.model.repository.db.MeasureMenuItem
import com.visionblue.business.ship.page.dialog.SelectionDialog
import com.visionblue.business.ship.page.dialog.SelectionItem
import com.visionblue.business.ship.util.FormatUtils
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_flow_menu_item.*

/**
 * @author: xlh
 * @date: 2020-07-30 5:42 PM
 * @desc:
 */
class FlowListAdapter : RecyclerView.Adapter<FlowListAdapter.VH>() {

    private val mItems = mutableListOf<MeasureMenuItem>()

    var onStateChangeListener: OnStateChangeListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_flow_menu_item, parent, false)
        return VH(itemView)
    }

    override fun getItemCount() = mItems.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        val flowItem = mItems[position]
        holder.flowView.setTypeText(flowItem.alias.toString())
        holder.flowView.setState(flowItem.state)
        if (flowItem.state == FlowViewLayout.WORKING) {
            holder.flowView.showWorking(
                volume = "",
                speed = "${FormatUtils.format3Decimal(flowItem.flowRate)}"+flowItem.speedUnit
            )
        }
        holder.flowView.onSwitchClickListener = object : FlowViewLayout.OnSwitchClickListener() {
            override fun onStart() {
                showSelectionDialog(holder.itemView.context, flowItem, holder.adapterPosition)
            }

            override fun onStop() {
                flowItem.state = FlowViewLayout.IDLE
                onStateChangeListener?.onWatchStop(flowItem)
            }
        }

    }

    private fun showSelectionDialog(
        context: Context,
        flowItem: MeasureMenuItem,
        adapterPosition: Int
    ) {
        SelectionDialog(
            context, listOf(
                // 含油污水
                SelectionItem(WasteTypeEnum.OIL_WASTE.desc, {
                    val isOk =  onStateChangeListener?.onIsContainListener(WasteTypeEnum.OIL_WASTE.code)
//                    flowOil.showWorking()
                    if (isOk!!){
                        changeItemToWorking(flowItem, adapterPosition, WasteTypeEnum.OIL_WASTE)
                    }

//                    mViewModel.startWatchOilPipeInfo(isOutput(), WasteTypeEnum.OIL_WASTE)
                }),
                // 残油
                SelectionItem(WasteTypeEnum.RESIDUAL_OIL.desc, {
                    val isOk =  onStateChangeListener?.onIsContainListener(WasteTypeEnum.RESIDUAL_OIL.code)
//                    flowOil.showWorking()
                    if (isOk!!) {
                        changeItemToWorking(flowItem, adapterPosition, WasteTypeEnum.RESIDUAL_OIL)
                    }
//                    flowItem.state = FlowViewLayout.WORKING
//                    notifyItemChanged(adapterPosition)
//                    onStateChangeListener?.onWatchStart(WasteTypeEnum.RESIDUAL_OIL)
//                    mViewModel.startWatchOilPipeInfo(
//                        isOutput(),
//                        WasteTypeEnum.RESIDUAL_OIL
//                    )
                }),
                // 生活污水

                SelectionItem(WasteTypeEnum.LIFE_WASTE.desc, {
                    val isOk =  onStateChangeListener?.onIsContainListener(WasteTypeEnum.LIFE_WASTE.code)
//                    flowOil.showWorking()
                    if (isOk!!) {
                        changeItemToWorking(flowItem, adapterPosition, WasteTypeEnum.LIFE_WASTE)
                    }
//                    flowItem.state = FlowViewLayout.WORKING
//                    notifyItemChanged(adapterPosition)
//                    onStateChangeListener?.onWatchStart(WasteTypeEnum.LIFE_WASTE)
//                    mViewModel.startWatchWaterPipeInfo(isOutput(), WasteTypeEnum.LIFE_WASTE)
                })
/*                // 含有毒液体物质的污水
                SelectionItem(WasteTypeEnum.HAZARDOUS_WASTE.desc, {
                    changeItemToWorking(flowItem, adapterPosition, WasteTypeEnum.HAZARDOUS_WASTE)
//                    flowItem.state = FlowViewLayout.WORKING
//                    notifyItemChanged(adapterPosition)
//                    onStateChangeListener?.onWatchStart(WasteTypeEnum.HAZARDOUS_WASTE)
//                    mViewModel.startWatchWaterPipeInfo(
//                        isOutput(),
//                        WasteTypeEnum.HAZARDOUS_WASTE
//                    )
                })*/
            )
        ).show()
    }

    private fun changeItemToWorking(
        flowItem: MeasureMenuItem,
        adapterPosition: Int,
        wasteTypeEnum: WasteTypeEnum
    ) {
        flowItem.state = FlowViewLayout.WORKING
//        flowItem.wasteTypeEnum = wasteTypeEnum
        notifyItemChanged(adapterPosition)
        onStateChangeListener?.onWatchStart(flowItem, wasteTypeEnum)
    }

    fun setData(items: List<MeasureMenuItem>?) {
        if (items != null) {
            mItems.clear()
            mItems.addAll(items)
            notifyDataSetChanged()
        }
    }

    fun setAllDisable() {
        mItems.forEach {
            it.state = FlowViewLayout.DISABLE
        }
        notifyDataSetChanged()
    }


    fun setAllIdle() {
        mItems.forEach {
            it.state = FlowViewLayout.IDLE
        }
        notifyDataSetChanged()
    }

    fun updateOil(oilFlowInfo: OilFlowInfo) {
        val index = mItems.indexOfFirst { it.isOil() && it.state == FlowViewLayout.WORKING }
        if (index >= 0) {
            mItems[index].flowRate = oilFlowInfo.instantFlowRate
            notifyItemChanged(index)
        }
    }

    //瞬时流速
    //todo
    fun updateWater(waterFlowInfo: WaterFlowInfo) {
        val index = mItems.indexOfFirst { it.isWater() && it.state == FlowViewLayout.WORKING }
        if (index >= 0) {
            mItems[index].flowRate = waterFlowInfo.instantFlowRate

            if(waterFlowInfo.instantFlowUnit.toString()=="LS"){
                mItems[index].speedUnit = ""
            }else{
                mItems[index].speedUnit = waterFlowInfo.instantFlowUnit.toString()
            }

            notifyItemChanged(index)
        }
    }

    fun updateWasteWater(waterFlowInfo: WaterFlowInfo) {
        val index = mItems.indexOfFirst { it.isWasteWater() && it.state == FlowViewLayout.WORKING }
        if (index >= 0) {
            mItems[index].flowRate = waterFlowInfo.instantFlowRate

            if(waterFlowInfo.instantFlowUnit.toString()=="LS"){
                mItems[index].speedUnit = ""
            }else{
                mItems[index].speedUnit = waterFlowInfo.instantFlowUnit.toString()
            }

            notifyItemChanged(index)
        }
    }

    fun updateSonic(ultraSonicFlowInfo: UltraSonicFlowInfo) {
        val index = mItems.indexOfFirst { it.isUltraSonic() && it.state == FlowViewLayout.WORKING }
        if (index >= 0) {
            mItems[index].flowRate = ultraSonicFlowInfo.instantFlowRate
            notifyItemChanged(index)
        }
    }

    interface OnStateChangeListener {
        fun onIsContainListener(type:String):Boolean
        fun onWatchStart(measureMenuItem: MeasureMenuItem, wasteTypeEnum: WasteTypeEnum)
        fun onWatchStop(measureMenuItem: MeasureMenuItem)
    }

    class VH(override val containerView: View) : RecyclerView.ViewHolder(containerView),
        LayoutContainer


}